package com.bric.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.bric.cacheFile.CacheFileScheduled;
import com.bric.cacheFile.HeaderFile;
import com.bric.prewarnFile.PrewarnFile;
import com.bric.sqlFile.SqlFile;
import com.bric.sqlFile.UpdateUploadPrice;
import com.bric.uploadFile.ReportFile;
import com.bric.util.StaticStr;

public class Scheduled {
	private static final Logger log=Logger.getLogger(Scheduled.class);
	public void service(){
			ScheduledExecutorService service = Executors.newScheduledThreadPool(7);
			DownloadUnZip downunzip=new DownloadUnZip();
			Thread tbefore=new Thread(downunzip);
			long oneDay=86400000;
			long bufferTime=12000;
			try {
				oneDay = Long.parseLong(StaticStr.period);
			} catch (Exception e) {
				log.info("间隔时间的参数类型格式不对，非法long值");
			}
			try {
				bufferTime=Long.parseLong(StaticStr.bufferTime);
			} catch (Exception e) {
				log.info("间隔时间的参数类型格式不对，非法long值");
			}
//			System.out.println(StaticStr.schfileTime+"  "+StaticStr.schzipUploadTime);
			long initDelay1=0;//执行下载和解压的任务的时间
			long initDelay2=bufferTime;//执行其他文件操作的时间，第二个时间一定延迟于第一个时间
			if(StaticStr.isExcute.equals("1")){
				initDelay1  = getTimeMillis(StaticStr.schStartTime) - System.currentTimeMillis();
				initDelay1=initDelay1<0?initDelay1+oneDay:initDelay1;
				initDelay2  = getTimeMillis(StaticStr.schStartTime) - System.currentTimeMillis()+bufferTime;
				initDelay2=initDelay2<0?initDelay2+oneDay:initDelay2;
			}
			if(StaticStr.unzipTask.equals("0")){
				service.scheduleAtFixedRate(tbefore, initDelay1, oneDay, TimeUnit.MILLISECONDS);
//				service.scheduleAtFixedRate(tbefore, initDelay1+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.sqlExcuteTask.equals("0")){
				SqlFile sql=new SqlFile();
				Thread t1=new Thread(sql);
				service.scheduleAtFixedRate(t1, initDelay2, oneDay, TimeUnit.MILLISECONDS);
//				service.scheduleAtFixedRate(t1, initDelay2+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.preDataMoveTask.equals("0")){
				PrewarnFile pre=new PrewarnFile();
				Thread t2=new Thread(pre);
				service.scheduleAtFixedRate(t2, initDelay2, oneDay, TimeUnit.MILLISECONDS);
//				service.scheduleAtFixedRate(t2, initDelay2+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.reportMoveTask.equals("0")){
				ReportFile rep=new ReportFile();
				Thread t3=new Thread(rep);
				service.scheduleAtFixedRate(t3, initDelay2, oneDay, TimeUnit.MILLISECONDS);
//				service.scheduleAtFixedRate(t3, initDelay2+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.cacheUploadTask.equals("0")){
				CacheFileScheduled sch=new CacheFileScheduled();
				Thread t4=new Thread(sch);
				service.scheduleAtFixedRate(t4, initDelay2, oneDay, TimeUnit.MILLISECONDS);
//				service.scheduleAtFixedRate(t4, initDelay2+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.moveHeaderFile.equals("0")){
				HeaderFile head=new HeaderFile();
				Thread t5=new Thread(head);
				service.scheduleAtFixedRate(t5, initDelay2, oneDay, TimeUnit.MILLISECONDS);
			}
			if(StaticStr.updateUploadPrice.equals("0")){
				UpdateUploadPrice uup=new UpdateUploadPrice();
				Thread t5=new Thread(uup);
//				service.scheduleAtFixedRate(t5, initDelay2+2*1000*60*60, oneDay, TimeUnit.MILLISECONDS);
				service.scheduleAtFixedRate(t5, 0, oneDay, TimeUnit.MILLISECONDS);
			}
	}
	
	/** 
	 * 获取指定时间对应的毫秒数 
	 * @param time "HH:mm:ss" 
	 * @return 
	 */  
	private static long getTimeMillis(String time) {  
	    try {  
	        DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
	        DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");  
	        Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);  
	        return curDate.getTime();  
	    } catch (ParseException e) {  
	        e.printStackTrace();  
	    }  
	    return 0;  
	}  
	public static void main(String[] args) {
		new Scheduled().service();
	}
}
